package com.fzu.ess.sys.service.Impl;

import com.fzu.ess.common.Const;
import com.fzu.ess.common.Model.JsonModel;
import com.fzu.ess.common.utils.SearchUtils;
import com.fzu.ess.ess.entity.Book;
import com.fzu.ess.sys.dao.MessageDao;
import com.fzu.ess.sys.entity.Message;
import com.fzu.ess.sys.entity.User;
import com.fzu.ess.sys.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * Created by ercha on 2015/10/25.
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private HttpSession session;

    @Override
    public boolean deleteBookMessage(String sender, String receiver, String reason) {
        Message m = new Message();
        m.setSender(sender);
        m.setReceiver(receiver);

//        System.out.println("sender: " + sender);
//        System.out.println("receiver: " + receiver);
//        System.out.println("reason: " + reason);

        m.setTitle("书籍删除通知");
        m.setContent("您的书籍因: \"" + reason + "\", 已被删除！");
        try {
            messageDao.save(m);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public JsonModel getDataByPage(Integer pageNumber, Integer pageSize, String filterText) {
        JsonModel j = new JsonModel();

        User user = (User)session.getAttribute(Const.SESSION.USER);

        StringBuilder hqlData = new StringBuilder("from Message b where ");
        String hqlCount;

        if(filterText!=null && filterText.length()>1){
            hqlData.append(SearchUtils.getSearchStr(filterText, "b"));
            hqlData.append(" and ");
        }

        hqlData.append(" b.delFlag = " + Const.DEL_FLAG_NORMAL + " and b.receiver = '" + user.getId()
                +"' order by b.createDate desc ");

        if(pageNumber==null || pageNumber<0){
            pageNumber = 0;
        }

        if(pageSize==null || pageSize<0){
            pageSize = 0;
        }

        System.out.println("hql: " + hqlData.toString());
        hqlCount = "select count(*) " + hqlData.toString();
        try {
            List<Message> bl = messageDao.find(hqlData.toString(), pageNumber, pageSize);
            j.setDataObj(bl);
            j.setTotal(messageDao.count(hqlCount));
            j.setRows(bl.size());
            return j;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public JsonModel setStatus(String id) {
        JsonModel j = new JsonModel();

        int res ;
        try {
            res = messageDao.executeHql("update Message b set b.status = " + Message.READ_STATUS + " where b.id = '" + id + "'");
            int cnt = (Integer)session.getAttribute(Const.SESSION.MESSAGE_CNT);
            cnt--;
            session.setAttribute(Const.SESSION.MESSAGE_CNT, cnt);
        } catch (Exception e){
            e.printStackTrace();
            return j;
        }
        j.setSuccess(res > 0);
        return j;
    }

    @Override
    public int unReadCnt() {
        User user = (User)session.getAttribute(Const.SESSION.USER);
        try{
            return messageDao.count("select count(*) from Message m where m.receiver = '" + user.getId() + "'"
            + " and m.status = " + Message.UNREAD_STATUS + " and m.delFlag = " + Const.DEL_FLAG_NORMAL);
        } catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

}
